#include "selfinfowidget.h"

/////////////////////////////////
/// personal information area
/////////////////////////////////

SelfInfoWidget::SelfInfoWidget(QWidget* parent)
    :QDialog(parent)
{
    // set necessary properties
    this->setFixedSize(500, 400);
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setWindowTitle("个人信息");
    this->setAttribute(Qt::WA_DeleteOnClose);   // 对话框关闭自动销毁对象
    this->move(QCursor::pos()); // 将窗口移动到光标的位置
    this->setStyleSheet("QWidget { background-color:rgb(230, 230, 230); }");

    layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(50, 0, 30, 0);
    layout->setAlignment(Qt::AlignCenter);
    this->setLayout(layout);

    // avatar
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(QSize(100, 100));
    avatarBtn->setIcon(QIcon(":/resource/image/default_avatar.png"));
    avatarBtn->setIconSize(QSize(100, 100));
    avatarBtn->setStyleSheet("QPushButton { border:none; }");

    QString tagStyle = "QLabel { font-size: 15px; font-weight:600; color:rgb(100, 100, 100); }";
    QString labelStyle = "QLabel { font-size: 20px; font-weight:400; color:rgb(100, 100, 100); }";

    // sequence
    idTag = new QLabel();
    idTag->setText("序号");
    idTag->setStyleSheet(tagStyle);

    idLabel = new QLabel();
    idLabel->setStyleSheet(labelStyle);

    // nickname
    nameTag = new QLabel();
    nameTag->setText("昵称");
    nameTag->setStyleSheet(tagStyle);

    nameLabel = new QLabel();
    nameLabel->setStyleSheet(labelStyle);

    nameEdit = new QLineEdit();
    nameEdit->setStyleSheet("QLineEdit { font-size: 20px; font-weight:400; "
                            "color:rgb(100, 100, 100); "
                            "border:1px solid color(1, 1, 1); }");
    nameEdit->hide();

    nameSubmitBtn = new QPushButton();
    nameSubmitBtn->setFixedSize(QSize(30, 30));
    nameSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    nameSubmitBtn->setIconSize(QSize(30, 30));
    nameSubmitBtn->setStyleSheet("QPushButton { border:none; }"
                                 "QPushButton:pressed { background-color:rgb(180, 180, 180); }");
    nameSubmitBtn->hide();

    // decription
    descTag = new QLabel();
    descTag->setText("签名");
    descTag->setStyleSheet(tagStyle);

    descLabel = new QLabel();
    descLabel->setWordWrap(true);
    descLabel->setStyleSheet(labelStyle);

    descEdit = new QLineEdit();
    descEdit->setStyleSheet("QLineEdit { font-size: 20px; font-weight:400; "
                            "color:rgb(100, 100, 100); "
                            "border:1px solid color(1, 1, 1); }");
    descEdit->hide();

    descSubmitBtn = new QPushButton();
    descSubmitBtn->setFixedSize(QSize(30, 30));
    descSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    descSubmitBtn->setIconSize(QSize(30, 30));
    descSubmitBtn->setStyleSheet("QPushButton { border:none; }"
                                 "QPushButton:pressed { background-color:rgb(180, 180, 180); }");
    descSubmitBtn->hide();

    // phone
    phoneTag = new QLabel();
    phoneTag->setText("电话");
    phoneTag->setStyleSheet(tagStyle);

    phoneLabel = new QLabel();
    phoneLabel->setStyleSheet(labelStyle);

    phoneEdit = new QLineEdit();
    phoneEdit->setStyleSheet("QLineEdit { font-size: 20px; font-weight:400; "
                            "color:rgb(100, 100, 100); "
                            "border:1px solid color(1, 1, 1); }");
    phoneEdit->hide();

    phoneSubmitBtn = new QPushButton();
    phoneSubmitBtn->setFixedSize(QSize(30, 30));
    phoneSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    phoneSubmitBtn->setIconSize(QSize(30, 30));
    phoneSubmitBtn->setStyleSheet("QPushButton { border:none; }"
                                 "QPushButton:pressed { background-color:rgb(180, 180, 180); }");
    phoneSubmitBtn->hide();

    // verify code
    verifyCode = new QLabel();
    verifyCode->setText("验证码");
    verifyCode->setStyleSheet(tagStyle);

    codeEdit = new QLineEdit();
    codeEdit->setFixedWidth(60);
    codeEdit->setStyleSheet("QLineEdit { border:1px solid rgb(1, 1, 1); font-size: 15px; font-weight:600; color:rgb(50, 50, 50); }");

    getCodeBtn = new QPushButton();
    getCodeBtn->setFixedHeight(30);
    getCodeBtn->setText("获取验证码");
    QString s = "QPushButton:pressed { background-color:rgb(180, 180, 180); }";
    s += "QPushButton { border:none; color:rgb(1, 1, 1); font-size:15px; }";
    getCodeBtn->setStyleSheet(s);

    // verifyCode->hide();
    codeEdit->hide();
    getCodeBtn->hide();

    // modify button
    QString btnStyle = "QPushButton:pressed { background-color:rgb(180, 180, 180); }";
    btnStyle += "QPushButton { border:none; }";

    nameModifyBtn = new QPushButton();
    nameModifyBtn->setFixedSize(QSize(30, 30));
    nameModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    nameModifyBtn->setIconSize(QSize(30, 30));
    nameModifyBtn->setStyleSheet(btnStyle);

    descModifyBtn = new QPushButton();
    descModifyBtn->setFixedSize(QSize(30, 30));
    descModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    descModifyBtn->setIconSize(QSize(30, 30));
    descModifyBtn->setStyleSheet(btnStyle);

    phoneModifyBtn = new QPushButton();
    phoneModifyBtn->setFixedSize(QSize(30, 30));
    phoneModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    phoneModifyBtn->setIconSize(QSize(30, 30));
    phoneModifyBtn->setStyleSheet(btnStyle);

#if TEST_F
    idLabel->setText("1234");
    nameLabel->setText("路飞");
    descLabel->setText("成为海贼王的男人");
    phoneLabel->setText("12345678");
    verifyCode->setText("验证码");
#endif

    // add to layout
    layout->addWidget(avatarBtn, 0, 0, 3, 1);

    layout->addWidget(idTag, 0, 1);
    layout->addWidget(idLabel, 0, 2);

    layout->addWidget(nameTag, 1, 1);
    layout->addWidget(nameLabel, 1, 2);

    layout->addWidget(descTag, 2, 1);
    layout->addWidget(descLabel, 2, 2);

    layout->addWidget(phoneTag, 3, 1);
    layout->addWidget(phoneLabel, 3, 2);

    layout->addWidget(nameModifyBtn, 1, 3);
    layout->addWidget(descModifyBtn, 2, 3);
    layout->addWidget(phoneModifyBtn, 3, 3);

    // connect signal slot
    initSignalSlot();

    // laod personal information
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    model::UserInfo* info = dataCenter->getMyself();
    if(dataCenter->getMyself() != nullptr) {
        avatarBtn->setIcon(info->avatar);
        idLabel->setText(info->userId);
        nameLabel->setText(info->nickname);
        descLabel->setText(info->description);
        phoneLabel->setText(info->phone);
    }
}

void SelfInfoWidget::initSignalSlot()
{
    connect(nameModifyBtn, &QPushButton::clicked, this, [=](){
        // remove original control
        layout->removeWidget(nameLabel);
        layout->removeWidget(nameModifyBtn);
        nameLabel->hide();
        nameModifyBtn->hide();
        // add new control
        layout->addWidget(nameEdit, 1, 2);
        layout->addWidget(nameSubmitBtn, 1, 3);
        nameEdit->show();
        nameSubmitBtn->show();
        nameEdit->setText(nameLabel->text());
    });

    connect(descModifyBtn, &QPushButton::clicked, this, [=](){
        // remove original control
        layout->removeWidget(descLabel);
        layout->removeWidget(descModifyBtn);
        descLabel->hide();
        descModifyBtn->hide();
        // add new control
        layout->addWidget(descEdit, 2, 2);
        layout->addWidget(descSubmitBtn, 2, 3);
        descEdit->show();
        descSubmitBtn->show();
        descEdit->setText(descLabel->text());
    });

    connect(phoneModifyBtn, &QPushButton::clicked, this, [=](){
        // remove original control
        layout->removeWidget(phoneLabel);
        layout->removeWidget(phoneModifyBtn);
        phoneLabel->hide();
        phoneModifyBtn->hide();
        // add new control
        layout->addWidget(phoneEdit, 3, 2);
        layout->addWidget(phoneSubmitBtn, 3, 3);
        phoneEdit->show();
        phoneSubmitBtn->show();
        phoneEdit->setText(phoneLabel->text());

        layout->addWidget(verifyCode, 4, 1);
        layout->addWidget(codeEdit, 4, 2);
        layout->addWidget(getCodeBtn, 4, 3);

        verifyCode->show();
        codeEdit->show();
        getCodeBtn->show();
    });

    connect(nameSubmitBtn, &QPushButton::clicked, this, [=](){
        QString newName = nameEdit->text();
        if(newName.isEmpty()) {
            return;
        }
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        connect(dataCenter, &model::DataCenter::modifyNicknameDone, this, &SelfInfoWidget::modifyNicknameDone, Qt::UniqueConnection);
        dataCenter->modifyNickNameAsync(newName);
    });

    connect(descSubmitBtn, &QPushButton::clicked, this, [=](){
        QString newDesc = descEdit ->text();
        if(newDesc.isEmpty()) {
            return;
        }
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        connect(dataCenter, &model::DataCenter::modifyDescriptionDone, this, &SelfInfoWidget::modifyDescriptionDone, Qt::UniqueConnection);
        dataCenter->modifyDescriptionAsync(newDesc);
    });

    connect(getCodeBtn, &QPushButton::clicked, this, [=](){
        QString newPhone = phoneEdit->text();
        if(newPhone.isEmpty()) {
            return;
        }
        phone = newPhone;
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        connect(dataCenter, &model::DataCenter::getVerifyCodeDone, this, &SelfInfoWidget::getVerifyCodeDone, Qt::UniqueConnection);
        dataCenter->getVerifyCodeAsync(newPhone);
        Toast::makeToast("verify code has been sent");
    });

    connect(phoneSubmitBtn, &QPushButton::clicked, this, [=](){
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        const QString& verifyCodeId = dataCenter->getVerifyCodeId();
        dataCenter->resetVerifyCodeId("");

        QString verifyCode = codeEdit->text();
        if(verifyCode.isEmpty()) {
            Toast::makeToast("please enter verify code");
            return;
        }
        connect(dataCenter, &model::DataCenter::modifyPhoneDone, this, &SelfInfoWidget::modifyPhoneDone, Qt::UniqueConnection);
        dataCenter->modifyPhoneAsync(phone, verifyCodeId, verifyCode);

        cnt = 1;
        codeEdit->setText("");
    });

    connect(avatarBtn, &QPushButton::clicked, this, [=](){
        QString filter("filter (*png *jpg)");
        QString path = QFileDialog::getOpenFileName(nullptr, "选择头像", nullptr, filter);
        if(path.isEmpty()) {
            return;
        }
        QByteArray image = model::readByteArrayFromFile(path);
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        connect(dataCenter, &model::DataCenter::modifyAvatarDone, this, &SelfInfoWidget::modifyAvatarDone, Qt::UniqueConnection);
        dataCenter->modifyAvatarAsync(image);
    });
}

void SelfInfoWidget::modifyNicknameDone()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();

    layout->removeWidget(nameEdit);
    nameEdit->hide();
    layout->addWidget(nameLabel, 1, 2);
    nameLabel->show();
    nameLabel->setText(dataCenter->getMyself()->nickname);

    layout->removeWidget(nameSubmitBtn);
    nameSubmitBtn->hide();
    layout->addWidget(nameModifyBtn, 1, 3);
    nameModifyBtn->show();
}

void SelfInfoWidget::modifyDescriptionDone()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();

    layout->removeWidget(descEdit);
    descEdit->hide();
    layout->addWidget(descLabel, 2, 2);
    descLabel->show();
    descLabel->setText(dataCenter->getMyself()->description);

    layout->removeWidget(descSubmitBtn);
    descSubmitBtn->hide();
    layout->addWidget(descModifyBtn, 2, 3);
    descModifyBtn->show();
}

void SelfInfoWidget::getVerifyCodeDone()
{
    getCodeBtn->setEnabled(false);
    QTimer* timer = new QTimer();
    cnt = 30;
    connect(timer, &QTimer::timeout, this, [=](){
        if(cnt == 1) {
            getCodeBtn->setEnabled(true);
            getCodeBtn->setText("获取验证码");
            timer->deleteLater();
        } else {
            cnt --;
            getCodeBtn->setText(QString::number(cnt) + "s");
        }
    });
    timer->start(1000);
}

void SelfInfoWidget::modifyPhoneDone()
{
    layout->removeWidget(verifyCode);
    layout->removeWidget(codeEdit);
    layout->removeWidget(getCodeBtn);
    verifyCode->hide();
    codeEdit->hide();
    getCodeBtn->hide();

    layout->removeWidget(phoneEdit);
    phoneEdit->hide();
    layout->removeWidget(phoneSubmitBtn);
    phoneSubmitBtn->hide();

    layout->addWidget(phoneLabel, 3, 2);
    phoneLabel->show();
    layout->addWidget(phoneModifyBtn, 3, 3);
    phoneModifyBtn->show();

    phoneLabel->setText(phone);
}

void SelfInfoWidget::modifyAvatarDone()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    avatarBtn->setIcon(dataCenter->getMyself()->avatar);
}


